//
//  ReservacionesViewController.m
//  Futbol5Final
//
//  Created by Johan Chaves on 4/14/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ReservacionesViewController.h"
#import "CanchaVO.h"
#import "ReservacionesCreator.h"
#import "ReservacionVO.h"

@implementation ReservacionesViewController

@synthesize calendarView,details,cancha,reservaciones;


-(id)initWithReservaciones:(CanchaVO *)canchap:(NSDictionary *)reservacionesData{
    
    self = [[[ReservacionesViewController alloc] initWithNibName:@"ReservacionesViewController" bundle:nil] autorelease];
    
    
     ReservacionesCreator *reservacionCreator = [[ReservacionesCreator alloc]init];
    
    
    
    reservaciones = [reservacionCreator createReservaciones:canchap:reservacionesData];
    
    cancha = canchap;
    
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void) loadView {
    [super loadView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.calendarView = [[[CXCalendarView alloc] initWithFrame: self.view.bounds] autorelease];
    [self.view addSubview: self.calendarView];
    self.calendarView.frame = self.view.bounds;
    self.calendarView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    self.calendarView.selectedDate = [NSDate date];
    
    self.calendarView.delegate = self;
}

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
    return YES;
}

#pragma mark CXCalendarViewDelegate

- (void) calendarView: (CXCalendarView *) calendarView
        didSelectDate: (NSDate *) date {
    
    ReservacionVO *reserTmp;
    
    NSEnumerator *arrenum = [reservaciones objectEnumerator];
    id cobj;   
    
    NSMutableArray *reservacionesPorFecha = [[NSMutableArray alloc]init];
    
    
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    
    //Set the required date format
    
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    //Get the string date
    
    NSString* dateString = [formatter stringFromDate:date];

    
    while ( cobj = [arrenum nextObject] ) {  
        reserTmp = cobj;
                        
        if([reserTmp.fecha isEqualToString:dateString]){
            
            [reservacionesPorFecha addObject:reserTmp];
        }
        
        
    }
    
    
    details = [[ReservacionesDetail alloc]initWithDate:date:reservacionesPorFecha:cancha];
    
    [self.navigationController pushViewController:self.details animated:YES];
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

@end
